package cn.edu.scau.dormitory_management_system.util;

import cn.edu.scau.dormitory_management_system.exception.pojo.MyException;
import cn.edu.scau.dormitory_management_system.enums.ExceptionMsgEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author djc
 * @date 2021/8/6
 * @time 17:35
 */
public class StringUtils {
    private static final List<String> list;

    static {
        list = new ArrayList<>();
        list.addAll(Arrays.asList("tb_admin_records","tb_bed","tb_bill",
                "tb_checkin_application","tb_checkin_audit","tb_checkout_application",
                "tb_checkout_audit","tb_department","tb_discipline","tb_dorm",
                "tb_dorm_adjust_audit","tb_dorm_adjustment_application","tb_dormbuild",
                "tb_flow","tb_flow_node","tb_messageboard","tb_permission","tb_register",
                "tb_repair","tb_role","tb_role_permission","tb_student","tb_supervisor",
                "tb_user","tb_user_role"));
    }

    public static boolean isEmpty(String value) {
        return value == null || "".equals(value);
    }

    private static String getTableName(String sql) {
        for (String str : list) {
            if(sql.contains(str)) {
                return str;
            }
        }
        throw new MyException(ExceptionMsgEnum.NO_MATCH_DB_TABLE);
    }

    public static String getOperation(String sql) {
        StringBuilder operation = new StringBuilder();

        operation.append("在表");
        operation.append(getTableName(sql));
        operation.append("中");

        if(sql.charAt(0) == 'i' || sql.charAt(0) == 'I') operation.append("插入");
        else if(sql.charAt(0) == 'd' || sql.charAt(0) == 'D') operation.append("删除");
        else operation.append("修改");

        operation.append("1条记录");
        return operation.toString();
    }

}
